package com.leigang.java.code.batch.config;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import sun.security.provider.certpath.OCSPResponse;

/**
 * @program: yaner-boot
 * @description: Job多实例练习
 * @author: leigang
 * @create: 2020-01-01 20:33
 */
@Configuration
@EnableBatchProcessing
public class JobDemo {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job jobDemoJob(){
        return jobBuilderFactory.get("jobDemoJob")
//                .start(stepOne())
//                .next(stepTwo())
//                .next(stepThree())
//                .build();
                .start(stepOne())
                .on("COMPLETED").to(stepTwo())
                .from(stepTwo()).on("COMPLETED").to(stepThree()) //fail()  stopAndRestart()
                .from(stepThree()).end()
                .build();
    }

    @Bean
    public Step stepOne(){
        return stepBuilderFactory.get("stepOne").tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                return RepeatStatus.FINISHED;
            }
        }).build();
    }

    @Bean
    public Step stepTwo(){
        return stepBuilderFactory.get("stepTwo").tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                return RepeatStatus.FINISHED;
            }
        }).build();
    }


    @Bean
    public Step stepThree(){
        return stepBuilderFactory.get("stepThree").tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                return RepeatStatus.FINISHED;
            }
        }).build();
    }
}
